from rest_framework import serializers
from django.contrib.contenttypes.models import ContentType
from . import models


# 数据库表序列化
class OftenMyToolSerializers(serializers.ModelSerializer):
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)

    class Meta:
        model = models.OftenMyTool
        fields = '__all__'


# 数据库表序列化
class AttachmentSerializers(serializers.ModelSerializer):
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)

    class Meta:
        model = models.Attachment
        fields = '__all__'


# 数据库表序列化
class DatabaseSerializers(serializers.ModelSerializer):
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)

    class Meta:
        model = models.Database
        fields = '__all__'


# 环境表序列化
class EnvironmentSerializers(serializers.ModelSerializer):
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)

    class Meta:
        model = models.Environment
        fields = '__all__'


# SQL明细表序列化
class SqlDetailSerializers(serializers.ModelSerializer):
    database_info = serializers.SerializerMethodField(read_only=True)  # 数据库表对应字段
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)

    def get_database_info(self, obj):  # 获取数据库表字段的对应方法
        return {
            'id': obj.database.id,
            'db_type':obj.database.db_type,
            'db_name': obj.database.db_name,  # 库名
            'db_ip': obj.database.db_ip,  # IP地址
            'db_port': obj.database.db_port,  # 端口号
            'db_user': obj.database.db_user,  # 用户名
            'db_password': obj.database.db_password,  # 密码
            'status': obj.database.status,
        }

    class Meta:
        model = models.SqlDetail
        fields = "__all__"  # 代表获取所有字段
        extra_kwargs = {
            "database": {"write_only": True},
        }


class ProjectSerializers(serializers.ModelSerializer):
    database_info = serializers.SerializerMethodField(read_only=True)  # 数据库表对应字段
    environment_info = serializers.SerializerMethodField(read_only=True)  # 数据库表对应字段
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)

    def get_database_info(self, obj):  # 获取数据库表字段的对应方法
        return {
            'id': obj.database.id,
            'db_name': obj.database.db_name,  # 库名
            'db_ip': obj.database.db_ip,  # IP地址
            'db_port': obj.database.db_port,  # 端口号
            'db_user': obj.database.db_user,  # 用户名
            'db_password': obj.database.db_password,  # 密码
            'status': obj.database.status,
        }

    def get_environment_info(self, obj):  # 获取环境表字段的对应方法
        son_obj = obj.environment
        return {
            'id': son_obj.id,
            'env_title': son_obj.env_title,  # 环境名称
            'env_protocol': son_obj.env_protocol,  # 请求协议
            'env_ip': son_obj.env_ip,  # IP地址
            'env_port': son_obj.env_port,  # 端口
            'env_domain': son_obj.env_domain,  # 域名
            'status': son_obj.status,
        }

    class Meta:
        model = models.Project
        fields = "__all__"  # 代表获取所有字段
        # depth = 1  # 以外键关系向下找1层，数字代表层数
        extra_kwargs = {
            "database": {"write_only": True},
            "environment": {"write_only": True},
        }


class ModuleSerializers(serializers.ModelSerializer):
    project_info = serializers.SerializerMethodField(read_only=True)  # 项目表对应字段
    testers_info = serializers.SerializerMethodField(read_only=True)  # 测试人员对应字段
    developer_info = serializers.SerializerMethodField(read_only=True)  # 测试人员对应字段
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)

    def get_project_info(self, obj):  # 获取表字段的对应方法
        project_obj = obj.project
        return {
            "id": project_obj.id,
            "name": project_obj.name,
            "describe": project_obj.describe,
            "status": project_obj.status,
        }

    def get_testers_info(self, obj):  # 获取表字段的对应方法
        myobj = obj.testers
        return {
            "id": myobj.id,
            "username": myobj.username,
            "name": myobj.name
        }

    def get_developer_info(self, obj):  # 获取表字段的对应方法
        myobj = obj.developer
        return {
            "id": myobj.id,
            "username": myobj.username,
            "name": myobj.name
        }

    class Meta:
        model = models.Module
        fields = "__all__"  # 代表获取所有字段
        # depth = 1  # 以外键关系向下找1层，数字代表层数
        extra_kwargs = {
            "project": {"write_only": True},
            "testers": {"write_only": True},
            "developer": {"write_only": True},
        }


class CasesSerializers(serializers.ModelSerializer):
    project_info = serializers.SerializerMethodField(read_only=True)  # 项目表对应字段
    test_person_info = serializers.SerializerMethodField(read_only=True)  # 用例负责人
    module_info = serializers.SerializerMethodField(read_only=True)  # 所属模块
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    execution_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    cases_count = serializers.SerializerMethodField(read_only=True)  # 所属模块

    def get_cases_count(self,obj):
        myobj = obj.casesdetail_set.all()
        return myobj.count()

    def get_project_info(self, obj):  # 获取表字段的对应方法
        myobj = obj.project
        return {
            "id": myobj.id,
            "name": myobj.name,
            "describe": myobj.describe,
            "status": myobj.status,
        }

    def get_test_person_info(self, obj):  # 获取表字段的对应方法
        myobj = obj.test_person
        return {
            "id": myobj.id,
            "username": myobj.username,
            "name": myobj.name
        }

    def get_module_info(self, obj):  # 获取表字段的对应方法
        myobj = obj.module
        return {
            "id": myobj.id,
            'developer': myobj.developer.username,
            'testers': myobj.testers.username,
            "name": myobj.name
        }

    class Meta:
        model = models.Cases
        fields = "__all__"  # 代表获取所有字段
        # depth = 1  # 以外键关系向下找1层，数字代表层数
        extra_kwargs = {
            "test_person": {"write_only": True},
            "module": {"write_only": True},
        }


class CasesDetailSerializers(serializers.ModelSerializer):
    cases_info = serializers.SerializerMethodField(read_only=True)  # 用例表对应字段
    content_object_info = serializers.SerializerMethodField(read_only=True)  # 表对象
    content_type_info = serializers.SerializerMethodField(read_only=True)  # 表对象
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    execution_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)

    def get_content_type_info(self, obj):
        myobj = obj.content_type
        if myobj:
            return {
                'id': myobj.id,
                'table_name': obj.content_object._meta.verbose_name,
            }
        else:
            return {}

    def get_content_object_info(self, obj):  # 获取表字段的对应方法
        myobj = obj.content_object
        return {
            "id": myobj.id,
            "title": myobj.title,
            'obj_key': myobj.obj_key,
            "table_name": myobj._meta.verbose_name
        }

    def get_cases_info(self, obj):  # 获取表字段的对应方法
        myobj = obj.cases
        return {
            "id": myobj.id,
            "name": myobj.name,
        }

    class Meta:
        model = models.CasesDetail
        fields = "__all__"  # 代表获取所有字段
        # depth = 1  # 以外键关系向下找1层，数字代表层数
        extra_kwargs = {
            "cases": {"write_only": True},
        }


class ContentTypeSerializers(serializers.ModelSerializer):
    table_name = serializers.SerializerMethodField(read_only=True)  # 对应的表名

    def get_table_name(self, obj):  # 获取表字段的对应方法
        content = ContentType.objects.filter(id=obj.id).first()
        model_class = content.model_class()
        return model_class._meta.verbose_name

    class Meta:
        model = models.ContentType
        fields = "__all__"  # 代表获取所有字段
        # depth = 1  # 以外键关系向下找1层，数字代表层数
        # extra_kwargs = {
        #     "cases": {"write_only": True},
        # }
